<template>
    <div>
      <div v-if="showLoading">
          <loadingPage />
      </div>
      <!-- <div v-if="showFace">
          <faceResoult :mode="faceMode" @submint="faceClick" :tips="faceTips" />
      </div> -->
      <div @click="returnTo" >{{ text }}</div>
     </div>
 </template>
 
 <script>
//  import Hybrid from '@/components/uzone-hybrid';
 import faceResoult from '@/components/faceResoult';
 import loadingPage from '@/components/loading-page';
 export default {
    props: {
    text: '',
      source: '',
      isActivity: false,
      pageTrackerInfo: {
        type: Object,
        default: () => {}
        }
    },
    components: {
      faceResoult,
      loadingPage,
    },
    data() {
      return {
        showLoading: false,
        faceMode: 'avatar',
        repeatFlag: 1,
        showFace: false,
        faceTips: 'Face comparison failed. Please click the button below to try again',
        disabledBtn: false
      }
    },
    methods: {
    async returnTo(){
        if (this.disabledBtn) return
        let user = this.$storage.get('user');
        if (this.source === 'home_loan') {
            // APP首页 - 申请按钮 - 点击
            this.$buryPoint.action({
                eventName: 'home_apply_click',
                eventValue: user && user.code ? { login: 1 } : { login: 0 },
                ...this.pageTrackerInfo
            });
        }
        await this.getDataCheck()
    },
    async getDataCheck() {
        if (this.repeatFlag == 0) {
            this.$toast("Today's upload times have been exhausted, please try again tomorrow!");
            return
        }
        this.disabledBtn = true
        let user = this.$storage.get('user');
        if (!user || !user.code) {
            this.disabledBtn = false
            this.$hybrid.action('gotologin');
            return
        } else {
            const res = await this.$api.getDataIntegrity().catch(res => {
                this.disabledBtn = false
            });
            let { status, error, data } = res;
            // 判断点击是否需要关闭当前炉头
            const pushRouterList = ['home_loan', 'mine_loan']
            const routerFN = pushRouterList.includes(this.source) ? 'push' : 'replace';
            if (status == '1' && error == '00000000') {
                //填资完成
                if (data.taskStatus == '1') {
                    if (this.isActivity) {
                        this.$router.push('/doubleDanEvent/info')
                    } else {
                        this.$router[routerFN]('/product/productList')
                    }
                } else if (data.taskStatus == '2') {
                    //go face 
                    let appVersion = this.$storage.get('appVersion');
                    // if (appVersion >= '1.0.4') {
                    if (appVersion >= '1.0.0') {
                        this.$router[routerFN]('/facecheck')
                    }
                    // else {
                    //     this.useDiscrean()
                    // }
                } else if (data.taskStatus == '3') {
                    let url = '';
                    if (data.USERBASEINFO != 1) {
                        url = '/information/identity';
                    } else if (data.USERPERSONALINFO != 1) {
                        url = '/information/personal';
                    } else if (data.USERJOB != 1) {
                        url = '/information/work';
                    } else if (data.USERCONTACT != 1) {
                        url = '/information/contact';
                    } else if (data.USERCARDID != 1) {
                        url = '/information/additional';
                    };
                    // TODO 
                    // APP首页 - 申请按钮 - 点击可返回
                    this.$router[routerFN](url)
                }else if(data.taskStatus == '4'){
                    let obj = {
                        faceMode:'back',
                        faceTips:"Today's upload times have been exhausted, please try again tomorrow!"
                    }
                    this.$router.push({pageTrackerInfo: me.pageTrackerInfo, path:'/faceCheckResult',query:obj, replace: this.source !== 'home_loan'})
                }
            }
        }
    },
    useDiscrean() {
        // const hybrid = new Hybrid(this.$os.android ? 'android' : 'ios');
        let me = this;
        this.$hybrid.action('discern', {
            param: {
                count: 1,
            },
            callback(data) {
                if (data.status == 1) {
                    if (data.data.status == 1) {
                        //face success
                        let params = {
                            type: 2
                        };
                        me.showFace = false;
                        me.showLoading = true;
                        me.associationFaceDetection(params)
                    } else {
                        //face fail
                        me.showLoading = false;
                        // me.showFace = true;
                        // me.faceTips = 'Please take another photo'
                        me.$router.push({path:'/faceCheckResult',query:{pageTrackerInfo: me.pageTrackerInfo, faceTips:'Please take another photo',faceMode:'avatar'}})
                    }
                }
            }
        });
    },

    associationFaceDetection(params) {
        let me = this;
        me.$api.associationFaceDetection(params).then(res => {
            me.showLoading = false;
            let { status, error, data } = res;
            if (status == '1' && error == '00000000') {
                let { status, repeatFlag } = data;
                if (status == 0) {
                    // face 1:1 fail
                    let query={faceMode:'',faceTips:""}
                    if (repeatFlag == 0) {
                        // 'dont try again'
                        query.faceMode = 'back';
                        query.faceTips = "Today's upload times have been exhausted, please try again tomorrow!";
                        // me.faceMode = 'back';
                        // me.faceTips = "Today's upload times have been exhausted, please try again tomorrow!";
                        // me.showFace = true;
                        // me.faceTips = ''
                    } else if (repeatFlag == 1) {
                        //  try again'
                        query.faceMode = 'avatar';
                        query.faceTips = "Please take another photo";
                        // me.faceMode = 'avatar';
                        // me.faceTips = "Please take another photo";
                        // me.showFace = true;

                    }
                    me.$router.push({path:'/faceCheckResult',query:query})
                } else if (status == 1) {

                  // 人脸识别独立页 - 人脸1比1 - 成功
                //   this.$hybrid.action('ee', {
                //     content: 'onlyFaceInfo_face_compare_success',
                //     value: '',
                //   });
                  me.$buryPoint.action({
                    eventName: 'onlyFaceInfo_face_compare_success',
                    ...me.pageTrackerInfo
                    });

                  if (this.isActivity) {
                        this.$router.push('/doubleDanEvent/info')
                    } else {
                        this.$router.push('/product/productList')
                    }
                }
            }
        })
    }
    }
 }
 </script>